package com.mf.interfaceTest;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;


import com.mf.MyApplication;
import com.mf.page.Actions;
import com.mf.page.IPageCallback;
import com.mf.page.menu.MainMenuData;
import com.mf.page.menu.MainMenuFunc;
import com.mf.page.menu.MainMenuPage;
import com.mf.mpos.pub.result.*;
import com.mf.mpos.pub.CommEnum;
import com.mf.mpos.pub.CommEnum.COMMRET;
import com.mf.mpos.pub.CommEnum.CONNECTMODE;
import com.mf.mpos.pub.Controler;
import com.mf.mpos.pub.EmvTagDef;
import com.mf.mpos.util.Misc;

public class InterfaceTestMenu {
	private static InterfaceTestMenu  instance = null;
	
	public static InterfaceTestMenu Instance(){
		if(instance == null){
			instance = new InterfaceTestMenu();
		}
		return instance;
	}
	
	/*
	 * 比较两个数组
	 */
	public static int memcmp(byte [] des , int des_i , byte[] src , int src_i ,  int len){	
		try {
			
			for(int i = 0; i < len ; i++){
				if(des[i + des_i] >  src[i + src_i])
					return 1;
				else if(des[i + des_i] <  src[i + src_i])
					return -1;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return 0;
	}
	SharedPreferences config = null;
	String title = null;
	public void init(Context  ac){
		MainMenuFunc.Instance().addMenuDealFunc( itemClickListener);
		
		MainMenuData data = MainMenuData.Instance();

		title = Controler.GetMode() == CONNECTMODE.BLUETOOTH ? "蓝牙模式":"音频模式";

		data.addItem("应用主菜单" ,	"连接模式设置" , "");
		//data.addItem("应用主菜单" , "通讯测试" ,"");
		data.addItem("应用主菜单" , "接口测试" ,"");
		data.addItem("应用主菜单" ,	"余额查询示例" , "");
		data.addItem("应用主菜单" ,	"DEMO版本" , "");
		data.addItem("接口测试" ,		"连接模式设置" , "");
		data.addItem("接口测试" ,		"连接终端" , "");
		data.addItem("接口测试" , 		"开启读卡器", "");
		data.addItem("接口测试",		"读磁条卡信息", "");
		data.addItem("接口测试",		"EMV", "");	
		//data.addItem("接口测试",		"输入密码", "");
		data.addItem("接口测试",		"计算密码", "");		
		data.addItem("接口测试",		"MAC计算", "");
		data.addItem("接口测试",		"获取设备信息", "");
		data.addItem("接口测试",		"获取随机数", "");
		data.addItem("接口测试",		"蜂鸣器",	 "");
		data.addItem("接口测试",		"设置时间", "");
		data.addItem("接口测试",		"获取时间", "");
		//data.addItem("接口测试",		"升级", "");
		data.addItem("接口测试",		"IC公钥下载(CAPK)", "");
		data.addItem("接口测试",		"IC参数下载(AID)", "");
		data.addItem("接口测试",		"KEK下载", "");
		data.addItem("接口测试",		"主密钥下载", "");
		data.addItem("接口测试",		"工作密钥下载", "");
		

		config = ac.getSharedPreferences("config", 1);
	}
	
	void connect( String audioconnectinfo , boolean show )
	{
		if( CONNECTMODE.AUDIO == Controler.GetMode() ){
			ConnectPosResult rc = Controler.connectPos(audioconnectinfo);	
			if( show){
				if( rc.bConnected ){
					blockmsg("提示", "连接成功");				
				}
				else{
					blockmsg("提示", "连接失败，请插入设备");	
				}			
			}			
		}
		else{
			Actions.ShowBluetoothSetPage();	
		}		
	}
	ActionItems acitems;
	int blockmsg(final String title, final String msg ){
		acitems.blockmsg(title, msg);
		return 0;
	}
	
	void showwait(final String title, final String msg){
		acitems.showwait(title, msg);
	}
	
	void closewait( ){
		acitems.closewait();
	}


	
	int testunconnecteditem(Activity activity, String id )
	{
		//无需连接的夜晚
		if(id.equals("连接终端"))
		{
			connect("", true);
			return 0;
		}
		else if( id.equals("连接模式设置") ){
			int r = acitems.select("请选择MPOS连接模式","蓝牙","音频");			
			CONNECTMODE mode = CONNECTMODE.BLUETOOTH;
			if( r == 1){
				mode = CONNECTMODE.AUDIO;
			}
			else{
				mode = CONNECTMODE.BLUETOOTH;
			}
			
			Controler.Init(activity,mode);
			Editor editor = config.edit();
			editor.putInt("CONNECTMODE", mode.ordinal());
			editor.commit();
			return 0;
		}
		else if(id.equals("余额查询示例"))
		{
			BalanceQuery.proc();
			return 0;
		}
		else if(id.equals("DEMO版本"))
		{
			String msg = "DEMO:20150116-01\nSDK版本:" + Controler.SDKVer();
			blockmsg(id, msg);
			return 0;
		}		
		return 1;
	}
	byte[] string2bcd(String key){
		int keyLen = key.length();			
		//转为bcd码，和62域一致
		if(keyLen %2 == 1 ){			// 长度奇数
			key = key + "0";		//左对齐
		}
		int bcdLen = (keyLen + 1) / 2;		
		byte[] bcdByte = new byte[bcdLen];	
		for (int i = 0; i < keyLen; i += 2) {
			try {
				bcdByte[i / 2] = (byte) Integer.decode("0x" + key.substring(i, i + 2)).intValue();
			} catch (Exception e) {
				// TODO: handle exception
				Log.v("error" , "asc2hex error");
			}
		}
		return bcdByte;
	}
	
	void testconnecteditem( String id )
	{
		String msg = "终端未连接,请先连接终端..";
		if( !Controler.posConnected())
		{			
			return ;
		}
		if( id.equals("开启读卡器") ){				
			showwait(id, "正在打开读卡器,请插卡或刷卡");
			OpenCardReaderResult result = Controler.OpenCardReader("消费", 100, 60, CommEnum.OPENCARDTYPE.COMBINED, "");
			msg = result.commResult.toDisplayName();
			if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
			{			
				msg = result.cardType.toDisplayName();	
			}				
		}
		else if( id.equals("读磁条卡信息" )){
			
				showwait(id, "正在打开读卡器，请刷卡");
				OpenCardReaderResult result = Controler.OpenCardReader("消费", 100, 60, CommEnum.OPENCARDTYPE.COMBINED, "");
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{	
					msg = result.cardType.toDisplayName();
					if( result.cardType == CommEnum.SWIPECARDTYPE.MAGCARD  ){
						showwait(id, "正在读取磁条卡信息");
						
						ReadMagcardResult result1 = Controler.ReadMagcard(CommEnum.READCARDTRACK.COMBINED,CommEnum.PANMASK.NOMASK);
						msg = result1.commResult.toDisplayName();
						if(result1.commResult.equals(CommEnum.COMMRET.NOERROR))
						{
							msg = "主账号:" + result1.sPan + "\n";
							msg += "卡有效期:" + result1.sExpData + "\n";
							String serviceCode = new String();
							for(int i = 0; i < 3; ++ i)
							{
								serviceCode += String.format("%02x", result1.serviceCode[i]);
							}
							msg += "服务码:" + serviceCode + "\n";
							msg += "磁道2:\n" + result1.sTrack2 + "\n";
							msg += "磁道3:\n" + result1.sTrack3 + "\n";
						}
					}						
				}
					
			
		}
		else if( id.equals("输入密码" )){
			
				showwait(id, "正在输入密码");
				InputPinResult result = Controler.InputPin((byte)6, (byte)60, "6222022010030311465");
				msg = result.commResult.toDisplayName();
				if( result.commResult == (CommEnum.COMMRET.NOERROR) )
				{
					String pin = new String();
					for(int i = 0; i < 8; ++ i)
					{
						pin += String.format("%02x", result.pinBlock[i]);
					}
					msg = pin;
				}
							
			
		}
		else if( id.equals("计算密码" )){
			
			showwait(id, "正在计算密码");		
			String src = "123456";
			CalPinResult result = Controler.CalPin( src.getBytes() , "123456789012345678") ;
			msg = result.commResult.toDisplayName();
			if( result.commResult == (CommEnum.COMMRET.NOERROR) )
			{
				String pin = new String();
				for(int i = 0; i < 8; ++ i)
				{
					pin += String.format("%02x", result.pinBlock[i]);
				}
				msg = "原密码 :"+src+"\n";
				msg += "计算结果 :"+pin+"\n";				
			}
		}
		else if( id.equals("MAC计算" )){
			
			String toMac = "123456789";
			byte[] toCalByte = toMac.getBytes();
			
			
				showwait(id, "正在计算mac");
				CalMacResult result = Controler.CalMac(CommEnum.MACALG.EBC, toCalByte,toCalByte.length);
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					String mac = new String();
					for(int i = 0; i < result.macvalue.length; ++ i)
					{
						mac += String.format("%02x", result.macvalue[i]);
					}
					msg = "计算mac的字符串：\n" + toMac + "\n";
					msg += "计算结果：\n" + mac + "\n";
				}
				else{
					msg = "出现异常" + result.commResult.name();
				}
			
			
			
		}
		else if( id.equals("获取设备信息") ){
			
				showwait(id, "正在读取设备信息");
				ReadPosInfoResult result = Controler.ReadPosInfo();
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = "机身号:" + result.sn  + "\n";
					msg += "状态:" + result.initStatus.toString() + "\n";
					msg += "程序版本号:" + result.posVer + "\n";
					msg += "电池状态:" + result.btype + "\n";
					msg += "数据版本号:" + result.dataVer + "\n";
					msg += "附加信息:" + result.customInfo + "\n";
				}					
			
			
			
		}
		else if( id.equals("获取随机数") ){
			
				showwait(id, "正在计算随机数");
				GetRandomResult result = Controler.GetRandomNum();
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					//转换为可见字符显示
					String randomNum = new String();
					for(int i = 0; i < result.randomNum.length; ++ i)
					{
						randomNum += String.format("%02x", result.randomNum[i]);
					}
					
					msg = "随机数:" + randomNum;
				}
				else{
					msg = "出现异常" + result.commResult.name();
				}
			
			
		}
		else if(id.equals("设置时间")){
			
				showwait(id, "正在设置时间");
				SetDateTimeResult result = Controler.SetDatetime("20150106135050");
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = "设置成功";
				}
				else{
					msg = "出现异常" + result.commResult.name();
				}
			
			
				
		}
		else if(id.equals("获取时间")){
			
				showwait(id, "正在获取时间");
				GetDateTimeResult result = Controler.GetDatetime();
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = "POS机当前时间:" + result.datetime;
				}
				else{
					msg = "出现异常" + result.commResult.name();
				}
			
			
							
		}
		else if(id.equals("蜂鸣器")){
			
				showwait(id, "正在蜂鸣");
				BeepResult result = Controler.Beep( 2, 50, 300, 1000);
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = "成功";
				}
			
			
		}
		else if(id.equals("升级")){
			
				showwait(id, "正在升级");
				UpdatePosProc updateProc = new UpdatePosProc();
				UpdatePosResult result = Controler.UpdatePos( MyApplication.getInstance().getApplicationContext() , "update.bin",updateProc);
				Log.v("uninpay", "after updatepos");
				if(result.bUpdateSucc)
				{
					msg = "升级成功";
				}
				else
				{	Log.v("uninpay", "GetErrorCode");
					switch(updateProc.GetErrorCode())
					{
					case CANCEL:
						msg = "用户取消";
						break;
					case FILENOTFOUND:
						msg = "文件未找到";
						break;
					case OTHERERROR:
						msg = "其他错误";
						break;
					}
					
					Log.v("uninpay", "After GetErrorCode");
				}
				
			Log.v("uninpay", "before showblockmsg");
				
		}
		else if(id.equals("IC卡卡号读取流程")){
			
				msg = "读取失败";
				showwait(id, "正在打开读卡器，请插卡");
				OpenCardReaderResult result = Controler.OpenCardReader("消费", 100, 60, CommEnum.OPENCARDTYPE.COMBINED, "");
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = result.cardType.toDisplayName();
					if(result.cardType.equals(CommEnum.SWIPECARDTYPE.ICCARD)) {							
						showwait(id, "正在读取IC卡信息");
						//消费金额10.00元，禁止使用电子现金，禁止离线，执行完整EMV流程
						StartEmvResult resultStart = Controler.StartEmv(1000,0,CommEnum.TRANSTYPE.FUNC_SALE,CommEnum.ECASHPERMIT.FORBIT, CommEnum.EMVEXECTYPE.FULL, CommEnum.FORCEONLINE.YES);
						msg = "Start Emv" + resultStart.commResult.toDisplayName();
						if(resultStart.commResult.equals(CommEnum.COMMRET.NOERROR))
						{
							//执行成功，打包55域显示
							//打包EMV数据
							List<byte[]> tags = new ArrayList<byte[]>();
							tags.add(EmvTagDef.EMV_TAG_5A_IC_PAN );
							tags.add(EmvTagDef.EMV_TAG_57_IC_TRACK2EQUDATA );
															
							GetEmvDataResult getResult = Controler.GetEmvData(tags);
							msg = "GetEmvData" + resultStart.commResult.toDisplayName();
							if(getResult.commResult.equals(CommEnum.COMMRET.NOERROR))
							{
								//转换为可见字符显示
								String asci = new String();
								for(int i = 0; i < getResult.tlvData.length; ++ i)
								{
									asci += String.format("%02x", getResult.tlvData[i]);
								}
								msg = "读取到的数据:\n" + asci;
							}
							
							Controler.EndEmv();
						}
					}
									
				}
			
			
			
		}
		else if(id.equals("EMV")){
			
				msg = "读取失败";
				showwait(id, "正在打开读卡器,请插卡");
				OpenCardReaderResult result = Controler.OpenCardReader("消费", 100, 60, CommEnum.OPENCARDTYPE.COMBINED, "");
				msg = result.commResult.toDisplayName();
				if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
				{
					msg = result.cardType.toDisplayName();
					if( result.cardType.equals(CommEnum.SWIPECARDTYPE.ICCARD) ){
						showwait(id, "正在读取IC卡信息");
						//消费金额10.00元，禁止使用电子现金，禁止离线，执行完整EMV流程
						StartEmvResult resultStart = Controler.StartEmv(1000,0,CommEnum.TRANSTYPE.FUNC_SALE,CommEnum.ECASHPERMIT.FORBIT, CommEnum.EMVEXECTYPE.FULL, CommEnum.FORCEONLINE.YES);
						msg = "StartEmv " + resultStart.commResult.toDisplayName();
						if(resultStart.commResult.equals(CommEnum.COMMRET.NOERROR))
						{
							//执行成功，打包55域显示						
							
							List<byte[]> tags = new ArrayList<byte[]>();
							tags.add(EmvTagDef.EMV_TAG_9F27_IC_CID );
							tags.add(EmvTagDef.EMV_TAG_9F10_IC_ISSAPPDATA );
							tags.add(EmvTagDef.EMV_TAG_9F37_TM_UNPNUM );
							tags.add(EmvTagDef.EMV_TAG_9F36_IC_ATC );
							tags.add(EmvTagDef.EMV_TAG_95_TM_TVR );
							
							
							GetEmvDataResult getResult = Controler.GetEmvData(tags);
							msg = "GetEmvData " + getResult.commResult.toDisplayName();
							if(getResult.commResult.equals(CommEnum.COMMRET.NOERROR))
							{
								//转换为可见字符显示
								String asci = new String();
								for(int i = 0; i < getResult.tlvData.length; ++ i)
								{
									asci += String.format("%02x", getResult.tlvData[i]);
								}
								msg = "读取到的数据:\n" + asci;
							}
							
							Controler.EndEmv();
						}
						else{
							msg = "出现异常" + result.commResult.name();
						}
					}
					else{
						msg = "请插入IC卡";
					}
				}
			
			
			
		}
		else if(id.equals("IC公钥下载(CAPK)"))
		{
			showwait(id, "正在清理");
			ICPublicKeyResult result = Controler.ICPublicKeyManage( CommEnum.ICPUBLICKEYACTION.CLEAR, null);
			msg = result.commResult.toDisplayName(); 
			if( result.commResult.equals(COMMRET.NOERROR) ){
				String[] keys = new String[]{
						"9F0605A0000003339F220108DF05083230333031323330DF060101DF070101DF028190B61645EDFD5498FB246444037A0FA18C0F101EBD8EFA54573CE6E6A7FBF63ED21D66340852B0211CF5EEF6A1CD989F66AF21A8EB19DBD8DBC3706D135363A0D683D046304F5A836BC1BC632821AFE7A2F75DA3C50AC74C545A754562204137169663CFCC0B06E67E2109EBA41BC67FF20CC8AC80D7B6EE1A95465B3B2657533EA56D92D539E5064360EA4850FED2D1BFDF040103DF0314EE23B616C95C02652AD18860E48787C079E8E85A"
						,"9F0605A0000003339F220109DF05083230333031323330DF060101DF070101DF0281B0EB374DFC5A96B71D2863875EDA2EAFB96B1B439D3ECE0B1826A2672EEEFA7990286776F8BD989A15141A75C384DFC14FEF9243AAB32707659BE9E4797A247C2F0B6D99372F384AF62FE23BC54BCDC57A9ACD1D5585C303F201EF4E8B806AFB809DB1A3DB1CD112AC884F164A67B99C7D6E5A8A6DF1D3CAE6D7ED3D5BE725B2DE4ADE23FA679BF4EB15A93D8A6E29C7FFA1A70DE2E54F593D908A3BF9EBBD760BBFDC8DB8B54497E6C5BE0E4A4DAC29E5DF040103DF0314A075306EAB0045BAF72CDD33B3B678779DE1F527"
						,"9F0605A0000003339F22010ADF05083230333031323330DF060101DF070101DF028180B2AB1B6E9AC55A75ADFD5BBC34490E53C4C3381F34E60E7FAC21CC2B26DD34462B64A6FAE2495ED1DD383B8138BEA100FF9B7A111817E7B9869A9742B19E5C9DAC56F8B8827F11B05A08ECCF9E8D5E85B0F7CFA644EFF3E9B796688F38E006DEB21E101C01028903A06023AC5AAB8635F8E307A53AC742BDCE6A283F585F48EFDF040103DF0314C88BE6B2417C4F941C9371EA35A377158767E4E3"
						,"9F0605A0000003339F22010BDF05083230333031323330DF060101DF070101DF0281F8CF9FDF46B356378E9AF311B0F981B21A1F22F250FB11F55C958709E3C7241918293483289EAE688A094C02C344E2999F315A72841F489E24B1BA0056CFAB3B479D0E826452375DCDBB67E97EC2AA66F4601D774FEAEF775ACCC621BFEB65FB0053FC5F392AA5E1D4C41A4DE9FFDFDF1327C4BB874F1F63A599EE3902FE95E729FD78D4234DC7E6CF1ABABAA3F6DB29B7F05D1D901D2E76A606A8CBFFFFECBD918FA2D278BDB43B0434F5D45134BE1C2781D157D501FF43E5F1C470967CD57CE53B64D82974C8275937C5D8502A1252A8A5D6088A259B694F98648D9AF2CB0EFD9D943C69F896D49FA39702162ACB5AF29B90BADE005BC157DF040103DF0314BD331F9996A490B33C13441066A09AD3FEB5F66C"
				};
				for( int j = 0; j < keys.length; j++){	
					String key =keys[j];
					
					showwait(id, "正在下载("+(j+1)+"/"+keys.length + ")");

					result = Controler.ICPublicKeyManage( CommEnum.ICPUBLICKEYACTION.ADD, string2bcd(key) );
					
					if( result.commResult.equals(CommEnum.COMMRET.NOERROR))
					{
						msg = "下载成功";
					}
					else
					{
						msg = result.commResult.toDisplayName(); 
						break;
					}
				}
			}
		}
		else if(id.equals("IC参数下载(AID)"))
		{
			ICAidResult result = Controler.ICAidManage( CommEnum.ICAIDACTION.CLEAR, null);		
			msg = result.commResult.toDisplayName(); 
			if( result.commResult.equals(COMMRET.NOERROR) ){
				String[] keys = new String[]{
					"9F0608A000000333010101DF0101019F08020020DF1105FCF0E40800DF1205FCF8E4F880DF130500100000009F1B0400002710DF150400000000DF160100DF170100DF14039F3704DF1801019F7B06000000080000DF1906000000050000DF2006000000100000DF2106000000010000"
					,"9F0608A000000333010102DF0101019F08020020DF1105FCF0E40800DF1205FCF8E4F880DF130500500000009F1B0400002710DF150400000000DF160100DF170100DF14039F3704DF1801019F7B06000000080000DF1906000000050000DF2006000000100000DF2106000000010000"
					,"9F0608A000000333010103DF0101019F08020020DF1105FCF0E40800DF1205FCF8E4F880DF130500100000009F1B0400002710DF150400000000DF160100DF170100DF14039F3704DF1801019F7B06000000080000DF1906000000050000DF2006000000100000DF2106000000010000"
					,"9F0608A000000333010106DF0101019F08020020DF1105FCF0E40800DF1205FCF8E4F880DF130500100000009F1B0400002710DF150400000000DF160120DF170110DF14039F3704DF1801019F7B06000000080000DF1906000000050000DF2006000000100000DF2106000000010000"
				};
				
				for( int j = 0; j < keys.length; j++){	
					String key =keys[j];
					showwait(id, "正在下载("+(j+1)+"/"+keys.length + ")");
					result = Controler.ICAidManage( CommEnum.ICAIDACTION.ADD, string2bcd(key));
					
					if( result.commResult.equals(CommEnum.COMMRET.NOERROR))
					{
						msg = "下载成功";
					}
					else
					{
						msg = result.commResult.toDisplayName(); 
						break;
					}
				}
			}
		}
		else if(id.equals("KEK下载"))
		{
			//测试数据是
			//使用默认的KEK  0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
			//对数据		  0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF
			//进行单倍长ECB加密得到的数据
			byte []kekD1 = {0x6d,(byte)0xce,(byte)0x0d,(byte)0xc9,(byte)0x00,(byte)0x65,(byte)0x56,(byte)0xa3};
			byte []kekD2 = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
			byte []kvc = {(byte)0xd5,(byte)0xd4,(byte)0x4f,(byte)0xf7};
			
			showwait(id, "正在下载KEK");
			
			LoadKekResult result = Controler.LoadKek( CommEnum.KEKTYPE.SIGNALE, kekD1,kekD2,kvc);
			msg = result.commResult.toDisplayName();
			if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				if( result.loadResult) {
					msg = "下载成功";
				}
				else{
					msg = "下载不成功";
				}
			}
			
			
		}
		else if(id.equals("主密钥下载"))
		{
			//测试数据是
			//使用生成的KEK  0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF
			//对数据		  0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
			//进行单倍长ECB加密得到的数据
			byte []keyD1 = {(byte)0x17,0x66,(byte)0x8d,(byte)0xfc,(byte)0x72,(byte)0x92,(byte)0x53,(byte)0x2d};
			byte []keyD2 = {(byte)0x00,0x00,(byte)0x00,0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00};
			byte []kvc = {(byte)0x82,(byte)0xE1,(byte)0x36,(byte)0x65};
			
			showwait(id, "正在下载主密钥");
			
			LoadMainKeyResult result = Controler.LoadMainKey( 
																		CommEnum.MAINKEYENCRYPT.KEK,
																		CommEnum.KEYINDEX.INDEX0,
																		CommEnum.MAINKEYTYPE.SIGNALE,
																		keyD1,keyD2,kvc);
			msg = result.commResult.toDisplayName();
			if(result.commResult.equals(CommEnum.COMMRET.NOERROR))
			{
				if( result.loadResult ){
					msg = "下载成功";
				}
				else{
					msg = "下载失败";
				}
			}
			
			
			
		}
		else if(id.equals("工作密钥下载"))
		{				
			//测试数据是
			//使用主密钥  0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11
			//对pin密钥  0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22
			// mac密钥   0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33
			//进行单倍长ECB加密得到的数据
			byte []pinkey = {(byte)0x98,0x50,(byte)0x63,(byte)0x42,(byte)0x25,(byte)0xDA,(byte)0x6A,0x55,(byte)0xF0,(byte)0x33,(byte)0x90,(byte)0xC1};
			byte []macKey = {(byte)0x36,0x68,(byte)0x51,(byte)0x1C,(byte)0x4C,(byte)0x65,(byte)0xE2,(byte)0xC9,(byte)0xD2,(byte)0x31,(byte)0xD9,(byte)0xEF};
		
			byte[] workKey = new byte[pinkey.length + macKey.length];
			int copyedLen = 0;
			System.arraycopy(pinkey, 0,workKey, copyedLen, pinkey.length);
			copyedLen += pinkey.length;
			System.arraycopy(macKey, 0,workKey, copyedLen, macKey.length);
			copyedLen += macKey.length;
			
			showwait(id, "正在下载工作密钥");
			
			//工作密钥为24位或40位或60位，即分别对应单倍长、双倍长、双倍长带磁道加密
			LoadWorkKeyResult result = Controler.LoadWorkKey( 
																		CommEnum.KEYINDEX.INDEX0,
																		CommEnum.WORKKEYTYPE.SIGNALE,
																		workKey,
																		workKey.length);
			msg = result.commResult.toDisplayName();
			
			if(result.commResult.equals(CommEnum.COMMRET.NOERROR) )
			{
				if(  result.loadResult ){
					msg = "下载成功";
				}
				else{
					msg = "下载失败";
				}					
			}
		}
		blockmsg(id, msg);		
	}
	
	
	void threadsleep(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	MainMenuFunc.OnClickMenuListener itemClickListener = new MainMenuFunc.OnClickMenuListener(){
		
		@Override
		public int OnClick(Activity activity, String id ,String title) {
			// TODO Auto-generated method stub
			acitems = new ActionItems(activity);
			
			if( testunconnecteditem(activity, id  ) == 0) {
				closewait();			
				return 0;
			}
			
			showwait(id, "正在连接...");
			//自动连接 自动复位一下
			if(Controler.posConnected())
			{						
				Controler.ResetPos();
			}
			else{
				connect("", false);				
			}
			
			if( !Controler.posConnected())
			{		
				blockmsg(id ,  "终端未连接,请先连接终端..");
			}
			else{
				testconnecteditem( id );
			}

			closewait();
			return 0;
		}
	};
}
